package com.liao.starapp;

import android.content.Context;
import android.view.View;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.GridView;

import androidx.fragment.app.Fragment;

import com.liao.starapp.luck.ItemLuckGridViewLayoutAdapter;
import com.liao.starapp.luck.Luck;
import com.liao.starapp.star.ItemStarGridViewLayoutAdapter;
import com.liao.starapp.utils.GetAssetsData;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


public class FragmentLuckLayoutFragment extends Fragment implements AdapterView.OnItemClickListener {

    private Context context;

    private GridView gridView;

    private ArrayList<Luck> lucks;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_luck_layout, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private void initView(View view) {
        gridView = view.findViewById(R.id.gridView);
        gridView.setOnItemClickListener(this);

        context = getContext();

        lucks = new ArrayList<>();
        String text = GetAssetsData.getGridViewText(context, "yunshis/yunshi.json");
        json(text);
        ItemLuckGridViewLayoutAdapter adapter = new ItemLuckGridViewLayoutAdapter(context, lucks);
        gridView.setAdapter(adapter);
    }

    private void json(String s) {
        try {
            JSONArray jsonArray = new JSONArray(s);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String logoname = jsonObject.getString("logoname");
                String name = jsonObject.getString("name");
                String zonghe = jsonObject.getString("zonghe");
                String aiqing = jsonObject.getString("aiqing");
                String shiye = jsonObject.getString("shiye");
                String caifu = jsonObject.getString("caifu");
                String jiankang = jsonObject.getString("jiankang");
                Luck luck = new Luck();
                luck.setLogoname(logoname);
                luck.setAiqing(aiqing);
                luck.setCaifu(caifu);
                luck.setJiankang(jiankang);
                luck.setName(name);
                luck.setShiye(shiye);
                luck.setZonghe(zonghe);
                lucks.add(luck);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}
